const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')
const app = getApp()
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    swiperCurrent: 0, //当前banner所在位置
    bannerList: [
      "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A6%96%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE/%E8%89%BE%E5%B0%94%E7%99%BB%E6%B3%95%E7%8E%AF.jpg?sign=65acfa5ec8373c4a3733505c8749d524&t=1687621621",
      "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A6%96%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE/%E6%BC%AB%E5%A8%81%E8%9C%98%E8%9B%9B%E4%BE%A0%E8%BF%88%E5%B0%94%E6%96%AF.jpg?sign=6e3702f281fe60a1519a0f99aada2f18&t=1687622418",
      "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A6%96%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE/%E5%8F%8C%E4%BA%BA%E6%88%90%E8%A1%8C.jpg?sign=595fdbd2f6800d59a7e566b63f54717b&t=1687621159",
      "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A6%96%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE/%E8%B5%9B%E5%8D%9A%E6%9C%8B%E5%85%8B2077.jpg?sign=edb10bebca0050982b5467ca203c68f0&t=1687622160",
      "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E9%A6%96%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%9B%BE/%E8%8D%92%E9%87%8E%E5%A4%A7%E9%95%96%E5%AE%A22.jpg?sign=7bcc3e543c33c1c60dbb3f00deb335c1&t=1687622923"], shopDetail: {
        latitude: 30.530243,
        longitude: 104.089588,
        name: "HappyBack主机游戏社",
        address: "四川成都市双流区观东一街666号4栋1单元213",
        bussinessTime: "12:00-22:00",
        contractPhone: "19302818452",


      },
    itemWidth: null,
    itemHeight: null,
    parentWidth: null,
    parentHeight: null,
    activityList: [
    ],
  },
  onShow() {

    const systemInfo = wx.getSystemInfoSync();
    var screenHeight = systemInfo.screenHeight;
    var screenWidth = systemInfo.screenWidth;
    var margin = screenWidth * 20 / 750;
    var itemWidth = (screenWidth - 3 * margin) / 2;
    var swiperHeight = 420 * screenWidth / 750;
    const windowHeight = systemInfo.windowHeight; // 可视区域高度
    const statusBarHeight = systemInfo.statusBarHeight; // 状态栏高度
    const navBarHeight = 44; // 顶部导航栏高度
    const tabBarHeight = 50; // 底部导航栏高度，注意这个值是固定的
    /**
     * 当我们自定义custiom的时候，底部导航栏不在可视区里面
     * https://blog.csdn.net/qq_46199553/article/details/126030693
     */
    const contentHeight = windowHeight - navBarHeight - statusBarHeight;
    const fontSize = 48 * screenWidth / 750

    var itemHeight = (contentHeight - margin * 4 - swiperHeight) / 4;
    var parentWidth = screenWidth / 2;
    var parentHeight = contentHeight - swiperHeight;
    this.setData({
      itemWidth,
      itemHeight,
      parentWidth,
      parentHeight,
      margin,
      fontSize
    })
    console.log(itemWidth, itemHeight)

  },
  swiperchange: function (e) { // banner滚动事件
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  // showCallNumber() {
  //   wx.makePhoneCall({
  //     phoneNumber: this.data.shopDetail.contractPhone // 电话号码
  //   });
  // },
  openWebview: function () {
    wx.navigateTo({
      url: '/pages/webview/webview'
    });
  },
  
  jumpToUrl(e) {
    var data=e.currentTarget.dataset.data;
    if (data.redirect_ulr) {
      if(data.name === '会员开通'){
        var userInfo=wx.getStorageSync("userInfo");
        
        if(!userInfo){
           this.setData({
            authVisible:true
           })
           return;
        }else if(userInfo.vip){
           wx.navigateTo({
            url: "/pages/vip/index"
           })  
           return;
        }
      }
      wx.navigateTo({
        url: data.redirect_ulr
      })
      
    } else {
      wx.showToast({
        title: "活动暂未开放"
      })
    }

  },
  goBuyVip(){
    wx.navigateTo({
      url: "/pages/buy-vip/index"
    })
  },
  goVipOrBuy() {
    var userInfo=wx.getStorageSync("userInfo");
    
        if(!userInfo){
           this.setData({
            authVisible:true
           })
           return;
        }else if(userInfo.vip){
           wx.navigateTo({
            url: "/pages/vip/index"
           })  
           return;
        }
        this.goBuyVip();
  },
  tapBanner(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.data, // 当前显示图片的链接，不填则默认为 urls 的第一张
      urls: this.data.bannerList // 需要预览的图片链接列表
    })
  },
  queryActivityList() {

    WXAPI.queryActivityList().then(res => {
      var activityList = res;

      this.setData({
        activityList: activityList,

      });
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  bindTypeTap: function (e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  onLoad: function (e) {

    app.queryUser();
    this.queryActivityList();

  },
  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  getCoupons: function () {
    WXAPI.coupons().then(res => {
      if (res.code == 0) {
        this.setData({
          coupons: res.data
        });
      }
    })
  },
  goGamelib(e) {


    wx.switchTab({
      url: '/pages/gamelib/index',
    })

    // Toast({
    //   context: this,
    //   selector: '#t-toast',
    //   message: '游戏库暂未建设完成',
    //   theme: 'warning',
    //   direction: 'column',
    // });
  },
  goMap(e) { // 打开地图
    var item = this.data.shopDetail
    wx.openLocation({
      latitude: item.latitude,
      longitude: item.longitude,
      name: item.name,
      address: item.address,
    })
  },
  callPhone(e) {
    const tel = e.currentTarget.dataset.tel
    wx.makePhoneCall({
      phoneNumber: tel
    })
  }
})